<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="js/jquery-3.5.1.js"></script>
</head>
<body >
    <div id="app" >
        <table border="1px " align="center" width="600px" height="300px" cellspacing="0">
            <tr align="center">
                <td>序号</td>
                <td>书籍名称</td>
                <td>出版日期</td>
                <td>价格</td>
                <td>购买数量</td>
                <td>操作</td>
            </tr>
            <tr align="center" v-for="(item,index) in items">
                <td>{{index+1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.date}}</td>
                <td  type="number">￥{{item.price}}</td>
                <td>
                    <input :disabled="item.num===1" type="button" value="-" @click="sub(index)">
                    {{item.num}}
                    <input type="button" value="+" @click="add(index)">
                </td>
                <td><input type="button" value="移除" @click="del(index)"></td>
            </tr>
            <tr>
                <td colspan="6" style="font-size: 1.2em;font-weight:bold" v-model.number="prices" type="number" >
                    总价格：￥ {{prices}}
                </td>
            </tr>
        </table>
    </div>  
</body>
<script>
    //初始化首行字体格式
   $(function(){
       $("tr:first td").css("font-family","微软雅黑").css("font-size","20px").css("font-weight","bold");
   })
   var app = new Vue({
       el:"#app",
       data:{
           items:[
               {
                   name:"《java编程思想》",
                   date:"2020-9",
                   price:98.00,
                   num:2
               },
               {
                   name:"《数据分析与数据原理》",
                   date:"2019-2",
                   price:39.00,
                   num:1
               },
               {
                   name:"《Hadoop权威指南》",
                   date:"2019-10",
                   price:59.00,
                   num:1
               },
               {
                   name:"《代码大全》",
                   date:"2018-8",
                   price:128.00,
                   num:1
               }
           ],
           prices:0.00
       },
       //初始化执行方法
       mounted:function () {
           this.sum();
       },
       methods:{
           del:function(i){
               //确认删除提示
               if(confirm("是否确认删除？")){
                      //总价 = 总价-单价*个数
                       var t =parseFloat(this.items[i].price)*parseInt(this.items[i].num);
                       this.prices = this.prices-t;
                       //删除一行元素  splice(index,个数)
                       this.items.splice(i,1);
                 }
           },
           sum:function(){
               //总价 = 单价*个数
               for(var i=0;i<this.items.length;i++){
                   this.prices+=parseFloat(this.items[i].price.toFixed(2))*parseInt(this.items[i].num);
               }
           },
           sub:function(i){
               //总价 = 总价-单价
               this.items[i].num -=1;
               this.prices -=parseFloat(this.items[i].price.toFixed(2));
           },
           add:function(i){
               //总价 = 总价+单价
               this.items[i].num +=1;
               this.prices+=parseFloat(this.items[i].price.toFixed(2));
           }
       }
   })
</script>
</html>